package com.nevermiss;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 自定义生日类
 * @author lee
 *
 */
public class Birthday {

	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	private Date date;

	/**
	 * @param birthday_date
	 *            生日Date
	 */
	public Birthday(Date birthday_date) {
		this.date = birthday_date;
	}

	/**
	 * 
	 * @param birthday_string
	 *            yyyy-MM-dd格式的生日日期字符串
	 */
	public Birthday(String birthday_string) {
		try {
			date = sdf.parse(birthday_string);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * 
	 * @return yyyy-MM-dd格式的生日字符串
	 */
	public String getBirthdayString() {
		return sdf.format(date);
	}

	/**
	 * 
	 * @return 生日距今还有多少天,已过或差20日以上返回-1，20日内返回具体天数
	 */
	public int getRemainDays() {
		Calendar birth = Calendar.getInstance();
		Calendar now = Calendar.getInstance();
		birth.setTime(date);
		birth.set(Calendar.YEAR, now.get(Calendar.YEAR) + 1);// 生日与当前年份比当前年份大1，比对现在是年底，生日在年初的情况
		int interdays = birth.get(Calendar.DAY_OF_YEAR)
				+ now.getMaximum(Calendar.DAY_OF_YEAR)
				- now.get(Calendar.DAY_OF_YEAR);
		if (interdays <= 20)
			return interdays;
		birth.set(Calendar.YEAR, now.get(Calendar.YEAR));// 生日与当前同年，便于比对
		interdays = birth.get(Calendar.DAY_OF_YEAR)
				- now.get(Calendar.DAY_OF_YEAR);
		if (interdays <= 20 && interdays >= 0)
			return interdays;
		return -1;
	}
}
